<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
define('Adz_class','1');
if(defined('MAGIC_QUOTES_GPC') && MAGIC_QUOTES_GPC == false){
	if($_GET['ac']!='diy' && !in_array($_GET['mod'],array('portalcp','spacecp'))){
		$_GET = daddslashes($_GET);
		$_POST = daddslashes($_POST);
	}
}
if(!defined('DISCUZ_VERSION')){define('DISCUZ_VERSION',$_G['setting']['version']);}

class Adz{
	public static function Amail($email,$title,$note){
		if(!function_exists('sendmail')) {
			include libfile('function/mail');
		}
		if(!sendmail($email, $title, $note)) {
			return false;
		}
		return true;
	}
	//对象转数组
	public static function obj_arr($obj){
		if(!is_object($obj)){return $obj;}
		foreach($obj as $k=>$v){
			if(is_string($k)){
				$k = (string)$k;
			}
			$arr[$k] = $v;
		}
		return $arr;
	}
	public static function arr_obj($arr){
		if(!is_array($arr)){return $arr;}
		foreach ($arr as $key => $val) {
			if(is_int($key) && $type != 'key'){
				$type == 'ini';
				$obj[] = Adz::arr_obj($val);
			}else if(!is_int($key)){
				if($type == 'ini'){
					unset($obj);
				}
				$type = 'key';
				$obj->$key = Adz::arr_obj($val);
			}
		}
		return $obj;
	}
	public static function url_charset($str){
		if(CHARSET=='gbk'){
			if(function_exists('mb_convert_encoding')){
				$str = mb_convert_encoding($str,'utf-8',CHARSET);
			}else{
				$str = iconv(CHARSET,'utf-8',$str);
			}
		}
		return $str;
	}
	
	public static function acan_ui($pname) {
		if(defined('Adz_acan_ui')){return;}
		define('Adz_acan_ui',1);
		$html = '<script src="source/plugin/'.$pname.'/acan/js/jquery.min.js"/></script>
<script src="source/plugin/'.$pname.'/acan/js/acan.js"/></script>
<script src="source/plugin/'.$pname.'/acan/js/lang/'.CHARSET.'.js"></script>
<style>@import url("source/plugin/'.$pname.'/acan/css/public.css");</style>';
		return $html;
	}
	//单条查询
	public static function fetch_con($table,$con,$select='*'){
		if(empty($con)) {
			$where = '1';
		} elseif(is_array($con)) {
			$where = DB::implode_field_value($con, ' AND ');
		} else {
			$where = $con;
		}
		return DB::fetch_first('SELECT '.$select.' FROM '.DB::table($table).' WHERE '.$where.' LIMIT 1');
	}
	//查询所有
	public static function fetch_con_All($table,$con,$select='*',$arr=array()) {
		if(empty($con)) {
			$where = '1';
		} elseif(is_array($con)) {
			$where = DB::implode_field_value($con, ' AND ');
		} else {
			$where = $con;
		}
		$limit = '';
		if($arr['limit']){
			$limit = ' LIMIT '.$arr['limit'];
		}
		return Adz::fetch_all('SELECT '.$select.' FROM '.DB::table($table).' WHERE '.$where.$limit);
	}
	//判断是插入还是更新
	public static function mysql_save($table,$data,$con) {
		$urs = Adz::fetch_con($table,$con);
		if(!$urs){
			return DB::insert($table,array_merge($data,$con));
		}else{
			return DB::update($table, $data, $con);
		}
	}
	public static function fetch_all($sql) {
		if(DISCUZ_VERSION=='X2.5'){
			return DB::fetch_all($sql);
		}elseif(DISCUZ_VERSION=='X2'){
			$data = array();
			$query = DB::query($sql);
			while($arr = DB::fetch($query)) {
				$data[] = $arr;
			}
			return $data;
		}
	}
	public static function auth($type='alias',$ex=1){
		global $_G;
		$group = unserialize($_G['cache']['plugin'][$type]['group']);
		if(in_array($_G['groupid'],$group) || $group[0]==''){
			if($_G['groupid']!=7)
			return true;
		}else{
			if($ex==1){
				echo Adz::charset('你所在的用户组没有权限');exit;
			}else{return false;}
		}
	}
	public static function auth_admin($group=array(1),$ex=1){
		global $_G;
		if(!in_array($_G['adminid'],$group)){
			if($ex==1){
				echo Adz::charset('你所在的用户组没有权限');exit;
			}else{return false;}
		}else{
			return true;
		}
	}
	public static function charset($str){
		global $_G;
		if(in_array($_G['charset'],array('gbk','gb2312','GBK','GB2312'))){
			$charsetto = "GBK";
		}elseif(in_array($_G['charset'],array('BIG5','big5'))){
			$charsetto = "BIG5";
		}else{
			$charsetto = false;
		}
		if(function_exists('mb_convert_encoding')){
			if($charsetto) $str = mb_convert_encoding($str,$charsetto,"UTF-8");
		}else{
			if($charsetto) $str = iconv("UTF-8",$charsetto.'//IGNORE',$str);
		}
		return $str;
	}
	public static function subdomain($item){
		global $_G;
		$domain = $_G['setting']['domain'];
		if($domain['app'][$item]){
			$return = $domain['app'][$item];
		}else{
			$return = $domain['app']['default'];
		}
		if(strlen($return)<3){
			$return = $_SERVER['HTTP_HOST'];
		}
		return $return;
	}
	//获取时间
	public static function get_time($type='datatime'){
		if($type == 'date'){
			$adatatime = date("Y-m-d",$_SERVER['REQUEST_TIME']);
		}elseif($type == 'time'){
			$adatatime = $_SERVER['REQUEST_TIME'];
		}elseif($type == 'datatime'){
			$adatatime = date("Y-m-d H:i:s",$_SERVER['REQUEST_TIME']);
		}else{
			$adatatime = date($type,$_SERVER['REQUEST_TIME']);
		}
		return $adatatime;
	}
	//时间戳转date
	public static function time_date($time,$type="Y-m-d H:i:s"){
		if($time > 0){
			return date($type,$time);
		}else{
			return date($type,$_SERVER['REQUEST_TIME']);
		}
	}
	//时间加减法
	public static function time_ex($time,$add=1,$type="d"){
		if(!is_numeric($time)){
			$time = strtotime($time);
		}
		//月份加减
		if($type=='m'){
			if($add>0){
				$daynum = date('t',$time);
			}else{
				$oldY = date('Y',$time);
				$oldm = date('n',$time);
				$oldp = $oldm+$add;
				if($oldp<1){
					$oldY += floor($oldp/12);
					$oldm = fmod($oldp, 12);
					if($oldm==0){$oldm = 12;$oldY--;}
				}elseif($oldp>0){
					$oldm = $oldp;
				}
				$daynum = date('t',strtotime($oldY.'-'.$oldm));
			}
			$newtime = $time+(86400*$daynum*$add);
			return $newtime;
			$oldY = date('Y',$time);
			$oldm = date('n',$time);
			$oldp = $oldm+$add;
			if($oldp>12){
				$oldY += floor($oldp/12);
				$oldm = fmod($oldp, 12);
				if($oldm==0){$oldm = 12;$oldY--;}
			}elseif($oldp<=12){
				$oldm = $oldp;
			}
			$oldo = date('d H:i:s',$time);
			$odate = $oldY.'-'.$oldm.'-'.$oldo;
		}elseif($type=='d'){
			$newtime = $time+(86400*$add);
			return $newtime;
			$oldY = date('Y',$time);
			$oldd = date('z',$time);
			$oldp = $oldd+$add;
			if(fmod($oldY, 4)==0){$Ynum = 366;}else{$Ynum = 365;}
			if($oldp>$Ynum){
				$oldY += floor($oldp/$Ynum);
				$oldd = fmod($oldp, $Ynum);
				if($oldd==0){$oldm = $Ynum;$oldY--;}
			}elseif($oldp<=$Ynum){
				$oldd = $oldp;
			}
		}
		return strtotime($odate);
	}
	
	//curl
	public static function curl($type='get',$url,$data,$gzip='1',$arr=array()){
		if($type=='get'){
			echo $url .= Adz::urlstr($data);
		}
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		if($arr['header']) curl_setopt($curl, CURLOPT_HTTPHEADER, $arr['header']);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
		if($gzip=='1'){
			curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
		}
		if($type=='post'){
			$post_data = Adz::urlstr($data);
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
		}
		$file_contents = $this->curl_redir_exec($curl);
		curl_close($curl);
		list($obj['header'], $obj['data']) = explode("\r\n\r\n", $file_contents, 2);
		return $obj;
	}
	public static function urlstr ($data,$parent=''){
		$str='';$add='';
		if(is_object($data) || is_array($data)){
			foreach ($data as $k=>$v){
				$add=$k;
				if($parent || (strlen($parent)==1 && $parent==0)){$add = $parent."[".$k."]";}
				if(is_object($data) || is_array($data)){
					$str .= Adz::urlstr($v,$add);
				}else{
					$str .= "&".$add."=".$v;
				}
			};
		}else{if($parent || (strlen($parent)==1 && $parent==0)){$str = "&".$parent."=".$data;}else{$str = "&".$data;}}
		return $str;
	}
	function curl_redir_exec($ch,$debug=""){
		static $curl_loops = 0;
		static $curl_max_loops = 20;
		if ($curl_loops++ >= $curl_max_loops){
			$curl_loops = 0;
			return FALSE;
		}
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$data = curl_exec($ch);
		
		$debbbb = $data;
		list($header, $data) = explode("\n\n", $data, 2);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if ($http_code == 301 || $http_code == 302){
			$matches = array();
			preg_match('/Location:(.*?)\n/', $header, $matches);
			$url = @parse_url(trim(array_pop($matches)));
			if (!$url){
				$curl_loops = 0;
				return $data;
			}
			$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
			$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
			curl_setopt($ch, CURLOPT_URL, $new_url);
			return $this->curl_redir_exec($ch);
		} else {
			$curl_loops=0;
			return $debbbb;
		}
	}




}
?>